package com.system.orders.service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.system.common.bean.SeckillOrders;
import com.system.common.exception.ServiceException;
import com.system.common.service.ISeckillOrdersService;
import com.system.orders.mapper.SeckillOrdersMapper;
import org.apache.dubbo.config.annotation.DubboService;

import java.util.List;

@DubboService
public class SeckillOrdersServiceImpl extends ServiceImpl<SeckillOrdersMapper, SeckillOrders> implements ISeckillOrdersService {

    @Override
    public SeckillOrders getById(Integer id) {
        return super.getById(id);
    }

    @Override
    public SeckillOrders getByUserIdAndActivityIdAndGoodsId(Integer userid, Integer activityid, Integer goodsid) {
        LambdaQueryWrapper<SeckillOrders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillOrders::getUserid, userid);
        return super.getOne(queryWrapper);
    }

    @Override
    public void addSeckillOrder(SeckillOrders seckillOrder) {
        super.save(seckillOrder);
    }

    @Override
    public void updateSeckillOrder(SeckillOrders seckillOrder) {
        super.updateById(seckillOrder);
    }

    @Override
    public void deleteSeckillOrder(Integer id) {
        SeckillOrders order = super.getById(id);
        if(order == null){
            throw new ServiceException("秒杀订单不存在");
        }
        if(order.getIsdel() == 1){
            throw new ServiceException("秒杀订单已删除");
        }
        super.removeById(id);
    }

    @Override
    public void updateStatus(Integer id, Integer status) {
        SeckillOrders order = super.getById(id);
        if(order == null){
            throw new ServiceException("秒杀订单不存在");
        }
        if(order.getIsdel() == 1){
            throw new ServiceException("秒杀订单已删除");
        }
        // 假如秒杀订单表也有状态字段，可加 order.setStatus(status);
        super.updateById(order);
    }

    @Override
    public List<SeckillOrders> getAll() {
        return super.list();
    }

    @Override
    public IPage<SeckillOrders> getPage(SeckillOrders seckillOrder, Page<SeckillOrders> page) {
        LambdaQueryWrapper<SeckillOrders> queryWrapper = new LambdaQueryWrapper<>();
        // 可加条件过滤，例如按userid/activityid/goodsid
        Page<SeckillOrders> resultPage = baseMapper.selectPage(page, queryWrapper);
        Long total = this.count(queryWrapper);
        resultPage.setTotal(total);
        return resultPage;
    }

    @Override
    public List<SeckillOrders> getByUserId(Integer userid) {
        LambdaQueryWrapper<SeckillOrders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillOrders::getUserid, userid);
        return super.list(queryWrapper);
    }

    @Override
    public List<SeckillOrders> getByActivityId(Integer activityid) {
        LambdaQueryWrapper<SeckillOrders> queryWrapper = new LambdaQueryWrapper<>();

        return super.list(queryWrapper);
    }

    @Override
    public SeckillOrders getByOrderNo(String orderNo) {
        LambdaQueryWrapper<SeckillOrders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SeckillOrders::getOrderno, orderNo);
        return super.getOne(queryWrapper);
    }
}
